ig.module(

    'game.movement'

).defines(
    function(){
        
        ig.movement = {};
        
        /*
            A base movement idea
        */
        BaseMovement = ig.Class.extend({
            /*
                entity that is moving
            */
            entity: null,
            
            init: function( entity ){
                this.entity = entity;
            },
            updatePosition: function(){
                
            },
            finished: function(){
                return false;
            }
        });
        ig.movement.BaseMovement = BaseMovement;
        
        ig.movement.LinearMovement = BaseMovement.extend({
            vel: {
                x: 0,
                y: 0
            },
            maxVel: 1000,
            
            accel: {
                x: 0,
                y: 0
            },            
            init: function( entity ){
                this.parent( entity );
            },
            updatePosition: function(){
                var tick = ig.system.tick;
                
                var dx = tick * this.vel.x;
                var dy = tick * this.vel.y;
                
                if( this.accel && ig.VUtils.len( this.entity.vel ) < this.maxVel ){
                    var dvx = tick * this.accel.x;
                    var dvy = tick * this.accel.y;
                    this.entity.vel = ig.VUtils.add( this.entity.vel, {x: dvx, y: dvy});                    
                }                
                
                this.entity.pos = ig.VUtils.add( this.entity.pos, {x: dx, y: dy});               
            }
        });
        
        /*
            TODO: evaluate to ElipticMovement            
        */
        ig.movement.RoundMovement = BaseMovement.extend({
            center: {
                x: 0,
                y: 0
            },
            /* angular and linear velocity */
            vel: {
                angle: 10,
                lin: {
                    x: 1,
                    y: 1
                }
            },
            /* angular and linear acceleration */
            accel: {
                angle: 10,
                lin: {
                    x: 1,
                    y: 1
                }
            },

            init: function( entity ){                
                this.parent( entity );
               
                /* vector from center to entity*/
                this.fromcnt = {x: this.center.x - this.entity.pos.x,
                                y: this.center.y - this.entity.pos.y};               
                
            },            
            updatePosition: function(){
                var tick = ig.system.tick;
                // turn from center vector
                this.fromcnt = ig.VUtils.turn( this.fromcnt, this.vel.angle * tick );
                
                // change angle speed
                if( this.accel.angle ){
                    this.vel.angle += tick * this.accel.angle;    
                }
                
                // move center
                if( this.vel.lin){                    
                    var dcx = tick * this.vel.lin.x;
                    var dcy = tick * this.vel.lin.y;
                    this.center = ig.VUtils.add( this.center, {x: dcx, y: dcy});                
                }
                
                // change center speed
                if( this.accel.lin){
                    var dcvx = tick * this.accel.lin.x;
                    var dcvy = tick * this.accel.lin.y;
                    this.vel.lin = ig.VUtils.add( this.vel.lin, {x: dcvx, y: dcvy });
                }
                
                this.entity.pos = ig.VUtils.add( this.center, this.fromcnt );                                  
            }
        });
        
        ig.movement.InsideRadiusMovement = ig.movement.LinearMovement.extend({
            center: {
                x: 0,
                y: 0
            },
            radius: 10,            
            centervel: {
                x: 0,
                y: 0
            },
            
            init: function( entity ){
                this.cstimer = new ig.Timer();
                this.parent( entity );
            },            
            updatePosition: function(){
                var dist = ig.VUtils.len({x: this.entity.pos.x - this.center.x,
                                          y: this.entity.pos.y - this.center.y});                

                if( this.cstimer.delta() > 2 ){
                    this.cstimer.set( 0 );
                    
                    if( dist >= this.radius){
                        this.vel = ig.VUtils.turn(ig.VUtils.inverse( this.vel ), Math.random() * 45 );
                        // turn back speed
                    }                    
                    this.center = ig.VUtils.add( this.center, this.centerstep);                    
                }                                
                this.parent();
            }            
        });        
        
        
        
        /*
            A chain of movements
            TODO: implement it
        */
        ig.movement.MovementChain = BaseMovement.extend({
        
        });
    }
);